package com.pj.dynamic;

import com.pj.util.DBContextHolder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
* @program: springboot-pure
* @ClassName DynamicDataSource
* @description:
* @author: gzf
* @create: 2023-11-22 14:27
* @Version 2.0
**/public class DynamicDataSource extends AbstractRoutingDataSource {


    @Value("${mini-db-router.jdbc.datasource.default}")
    private String defaultDataSource;

    @Override
    protected Object determineCurrentLookupKey() {
        if (null == DBContextHolder.getDBKey()) {
            return defaultDataSource;
        } else {
            return "db" + DBContextHolder.getDBKey();
        }
    }
}
